package org.example.MVC;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

//页面跳转
@Controller
public class PageController {
    @RequestMapping("/register")
    public void showPageByVoid(){
        System.out.println("showPageByVoid running");
    }

    @RequestMapping("/showPageByString")
    public String showPageByString(){
        return "register";
    }

    @RequestMapping("/showPageByForward")
    public String showPageByForward(){
        System.out.println("showPageByForward running");
        return "forward:orders.jsp";           //请求转发（一次请求）
    }

    @RequestMapping("/showPageByRedirect")
    public String showPageByRedirect(){
        System.out.println("showPageByRedirect running");
        return "redirect:http://www.itheima.com";           //请求重定向（两次请求）
    }
    //方法返回的字符串一旦添加了"forward:"或"redirect:"前缀，视图解析器便不再会为方法返回值拼接前缀和后缀了

    @RequestMapping("/showPageByRequest")
    public String showPageByRequest(HttpServletRequest request){   //request是代表HTTP请求信息的对象 ,(客户请求时创建该对象); response是代表HTTP响应信息的对象,(服务器响应时创建该对象)
        System.out.println("showPageByRequest running");
        request.setAttribute("username","request");
        return "register";
    }

    @RequestMapping("/showPageByModel")
    public String showPageByModel(Model model){
        System.out.println("showPageByModel running");
        model.addAttribute("username","model");
        User user = new User();
        user.setPassword("password");
        model.addAttribute("user",user);
        return "register";
    }
}
